import Vue from 'vue'
import Vuex from 'vuex'

import { addCart, getCartInfo } from '../api/cart'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    /* isShow: true, */ // 控制头部和脚步组件的显示
    login: false, //是否登录
    userInfo: null, // 用户的信息
    cartList: [], //加入购物车的商品
    addCartSuccess: true, // 判断加入购物车是否成功
    showCart: false
  },
  //准备actions——用于响应组件中的动作
  actions: {
    // 以后会使用 --2021/12/15
    /* async ADDCART({ commit, state }, goodsInfo){
      const { data: res } = await addCart(goodsInfo)

      if(res.code !== 0) {
        return state.addCartSuccess = false
      }

      const { data: res2 } = await getCartInfo()

      state.cartList = res2.result.list

      // console.log(state.cartList)
      return state.addCartSuccess = true
    }, */
  },
  //准备mutations——用于操作数据（state）
  mutations: {
    /* SHOWCOM(state, { showCom }) {
      state.isShow = showCom
    }, */
    SHOWCART(state, { showCart }) {
      state.showCart = showCart
      // console.log(state.cartList)
    },
    CARTINFO(state, cartList) {
      cartList.forEach(item => {
        if(item.goods_number > item.goods_info.goods_num) {
          item.selected = false
        }
      })
      state.cartList = cartList
      // console.log(cartList)
    },
    async ADDCART(state, goodsInfo){
      const { data: res } = await addCart(goodsInfo)

      if(res.code !== 0) {
        return state.addCartSuccess = false
      }

      const { data: res2 } = await getCartInfo()

      state.cartList = res2.result.list

      // console.log(state.cartList)
      return state.addCartSuccess = true
    },
    ISLOGIN(state, info) {
      state.userInfo = info;
      state.login = true;
      // console.log(state.userInfo)
    },
  },
  modules: {
  }
})
